package com.generator.common;

/**
 * ${description}
 *
 * @author jaybril
 * @date 2019-06-01 22:01
 **/

import com.generator.common.bean.DatabaseBean;
import com.generator.common.bean.GeneratorConfigBean;
import com.generator.common.bean.SystemConfigBean;
import com.generator.common.util.DBUtil;
import com.generator.model.DbConnections;
import com.generator.view.bean.GeneratorConfig;
import com.generator.view.util.BeanUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MysqlUtil {
    private static String DB_CONNECTIONS = "db_connections";
    private static String GENERATOR_CONFIG_LIST = "generator_config_list";
    private static String SYSTEM_CONFIG = "system_config";


    public static boolean isExistSystemConfig(String name) {
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("name", name);
        return isExist(whereMap, SYSTEM_CONFIG);
    }

    public static boolean saveSystemConfig(SystemConfigBean systemConfigBean) {
        return save(systemConfigBean, SYSTEM_CONFIG);
    }

    public static boolean removeSystemConfig(String name) {
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("name", name);
        return delete(whereMap, SYSTEM_CONFIG);
    }

    public static boolean updateSystemConfig(SystemConfigBean systemConfigBean) {
        return update(systemConfigBean, SYSTEM_CONFIG);
    }

    public static SystemConfigBean selectSystemConfig(String name) {
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("name", name);
        SystemConfigBean systemConfigBean = new SystemConfigBean();
        try {
            List<Map<String, Object>> query = select(whereMap,SYSTEM_CONFIG);
            if (query != null && query.size() > 0) {
//                BeanUtils.copyProperties(query.get(0), systemConfigBean);
                BeanUtils.copy(query.get(0),systemConfigBean);
                return systemConfigBean;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean saveDbConnection(DbConnections dbConnections) {
        return save(dbConnections, DB_CONNECTIONS);
    }

    public static boolean removeDbConnection(String id) {
        return delete(DB_CONNECTIONS, id);
    }

    public static boolean deleteConfig(String id) {
        return delete(SYSTEM_CONFIG, id);
    }

    public static boolean updateDbConnection(DbConnections dbConnections) {
        return update(dbConnections, DB_CONNECTIONS);
    }

    public static DatabaseBean selectOneDbConnection(String id) {
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("id", id);
        List<Map<String, Object>> mapList = select(whereMap, DB_CONNECTIONS);

        DatabaseBean databaseBean = new DatabaseBean();
        if (mapList != null && mapList.size() > 0) {
            try {
                BeanUtils.copy(mapList.get(0), databaseBean);
                return databaseBean;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    public static DatabaseBean selectDbConnectionByName(String name) {
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("name", name);
        List<Map<String, Object>> mapList = select(whereMap, DB_CONNECTIONS);

        DatabaseBean databaseBean = new DatabaseBean();
        if (mapList != null && mapList.size() > 0) {
            BeanUtils.copyProperties(mapList.get(0), databaseBean);
            return databaseBean;
        }
        return null;
    }

    public static Boolean saveGeneratorConfig(GeneratorConfigBean generatorConfig){
        return save(generatorConfig,GENERATOR_CONFIG_LIST);
    }

    public static Boolean removeGeneratorConfig(String name){
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("name", name);
        return delete(whereMap,GENERATOR_CONFIG_LIST);
    }

    public static Boolean updateGeneratorConfig(GeneratorConfigBean generatorConfig){
        return update(generatorConfig,GENERATOR_CONFIG_LIST);
    }

    public static List<GeneratorConfig> selectGeneratorConfigAll(String configId){
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("configId", configId);
        return BeanUtils.copyList(select(whereMap,GENERATOR_CONFIG_LIST),GeneratorConfig.class);
    }

    public static GeneratorConfigBean selectOneGeneratorConfig(String name) {
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("name", name);
        List<Map<String, Object>> mapList = select(whereMap, GENERATOR_CONFIG_LIST);
        GeneratorConfigBean generatorConfigBean = new GeneratorConfigBean();
        if (mapList != null && mapList.size() > 0) {
            BeanUtils.copyProperties(mapList.get(0), generatorConfigBean);
            return generatorConfigBean;
        }
        return null;
    }


    public static List<DatabaseBean> selectAllDbConnection(String configId){
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("config_id", configId);
        List<Map<String, Object>> mapList = select(whereMap, DB_CONNECTIONS);
        return BeanUtils.copyList(mapList,DatabaseBean.class);
    }

    public static Boolean isExistGeneratorConfig(String name){
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("name", name);
        return isExist(whereMap,GENERATOR_CONFIG_LIST);
    }


    private static <T> boolean save(T t, String dbName) {
        try {
            Map<String, Object> map = BeanUtils.objectToMap(t);
            DBUtil.insert(dbName, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private static boolean delete(String dbName, String id) {
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("id", id);
        try {
            DBUtil.delete(dbName, whereMap);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    private static <T> boolean update(T t, String dbName) {
        try {
            Map<String, Object> map = BeanUtils.objectToMapWithOutNull(t);
            Map<String, Object> whereMap = new HashMap<>(1);
            whereMap.put("id", map.get("id"));
            DBUtil.update(dbName, map, whereMap);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private static List<Map<String, Object>> select(Map whereMap, String dbName) {
        try {
            return DBUtil.query(dbName, whereMap);

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Boolean isExist(Map whereMap, String dbName) {
        List<Map<String, Object>> mapList = select(whereMap, dbName);
        if (mapList != null && mapList.size() > 0) {
            return true;
        }
        return false;
    }

    private static Boolean delete(Map whereMap, String dbName) {
        try {
            DBUtil.delete(dbName, whereMap);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

}

